﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using Silvermoon.Controls;
using Passwords.Biz;
using Passwords.Controls;
using Silvermoon.Shapes;
using System.Drawing;
using Silvermoon.Windows;
using Passwords.Models;
using Passwords.Utils;
using Silvermoon.OpenGL;
using Silvermoon.Images;

namespace Passwords.Pages
{
    /// <summary>
    /// Page to selected the DisplayIndex of a PasswordLabel.
    /// The page has a Picker control to browse to the location where to insert the label which uses a small horizontal line as lense to mark
    /// the insert location between to items.
    /// There is also a label centered, which indicates the name of the PasswordLabel to insert.
    /// </summary>
    public class LabelIndexPicker : Page
    {
        private PasswordLabel label;
        private Picker picker;
        private Label center;

        public LabelIndexPicker()
            : base()
        {
            InitializePage();
        }

        protected override void Dispose(bool dispose)
        {
            base.Dispose(dispose);
            IndexItem.DisposeItems();
        }


        public PasswordLabel Label
        {
            get { return label; }
            set
            {
                if (label != value)
                {
                    label = value;
                    OnLabelChanged(value);
                }
            }
        }

        private void OnLabelChanged(PasswordLabel label)
        {
            if (label != null)
            {
                var source = (from l in label.Context.Labels
                              where l.Id != label.Id
                              orderby l.DisplayIndex
                              select new PickerData((l.DisplayIndex + 1).ToString(), l.Name, l.DisplayIndex)).ToList();

                source.Add(new PickerData((source.Count).ToString(), string.Empty, source.Count + 1));
                picker.ItemsSource = source;
                picker.SelectedItem = source[label.DisplayIndex];
                this.center.Text = label.Name;
            }
        }

        private void InitializePage()
        {
            Title = "MOVE LABEL";
            picker = new Picker
            {
                Mode = PickerMode.None,
                Margin = new Margin(10, 4, 10, 0),
                IsCyclic = false,
                TextColor = PhoneColors.White,
                ItemHeight = 100,
            };
            picker.Styles.SetClass<Item>(StylePool.LabelIndexPickerStyle);

            // in this case, the lense is just a line which indicates  the location where to insert the item:
            picker.Lense = new RectangleShape { Size = new Size(400, 4), Color = PhoneColors.Accent };
            picker.LenseMargin = new Margin(0, 0, 0, null);

            center = new Label { Margin = new Margin(10, 0, 10, 48), IsHitTestVisible = false, Alignment = Alignment.Far, TextColor = PhoneColors.Accent };
            Controls.Add(picker, center);

            picker.GetVirtualItem += new EventHandler<PickerItemEventArgs>(picker_GetVirtualItem);

            Toolbar tb = new Toolbar();
            tb.AddButton(SysImages.Ok).Tap += new EventHandler<TapEventArgs>(OnOk);
            tb.AddButton(SysImages.Cancel, Page.BackCommand);
            Toolbar = tb;
        }

        void OnOk(object sender, TapEventArgs e)
        {
            if (label != null && picker.SelectedItemIndex >= 0)
            {
                label.DisplayIndex = picker.SelectedItemIndex;
                GoBack();
            }
        }

        private static readonly Margin itemMargin = new Margin(0, 4, 0, 0);

        void picker_GetVirtualItem(object sender, PickerItemEventArgs e)
        {
            IndexItem item = IndexItem.GetItem();
            PickerData data = e.Value as PickerData;
            item.Margin = itemMargin;
            item.TextColor = PhoneColors.White;
            item.DataValue = e.Value;
            item.Font = FontCache.MediumRegularFont;
            item.Name = data.Value;
            item.Index = data.IntVal;
            e.Container = item;
        }

    }
}
